<?php 
namespace XFCore\traits\admin;
use lib\Examine;
trait Wechat_TrumpetTrait{
    public function Wechat_Trumpetstatus(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = input("post")['id'];
        if(empty($id))exitjson(-1,'非法操作！');
        $row = $this->db->wechat_trumpetrow($id,'id','status');
        if(empty($row))exitjson(-1,'数据不存在');
        $data = [
            'status'=>$row['status'] == 1 ? 0 : 1
        ];
        if($this->db->update("wechat_trumpet",$data,['id'=>$id]))exitjson(200,'更新状态成功！');
        exitjson(-1,'更新失败！');
    }
    public function Add_Wechet_Tp($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        // 验证必填参数
        $requiredFields = [
            'mode' => '收款模式',
            'wx_user' => '微信账号',
            'wx_name' => '微信昵称',
            'sort' => '排序'
        ];
    
        $params = $this->validateAndPrepareData($data, $requiredFields);
        if($this->db->insert('wechat_trumpet',$params))exitjson(200,'添加成功！');
        exitjson(-1,'添加失败！');
    }
    public function Edit_Wechet_Tp($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        Examine::checkRequiredParams(array($data['wx_user'] , $data['wx_name'] , $data['sort'] , $data['id']));
        $id = $data['id'];
        unset($data['id']);
        if(empty($this->db->wechat_trumpetrow($id,'id','wx_user')))exitjson(-1,'微信店员不存在');
        $row = $this->db->wechat_trumpetrow($data['wx_user'],'wx_user','id');
        if(!empty($row) && $row['id'] != $id)exitjson(-1,'该微信号已存在请勿重复添加');
        if($this->db->update("wechat_trumpet",$data,['id'=>$id]))exitjson(200,'修改成功！');
        exitjson(-1,'修改失败！');
    }
    public function wxpay_zt_Out($get,$post,$data){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $wx_username   = $data['wx_username'];
        $wx_dyname     = $data['wx_dyname'];
        Examine::checkRequiredParams(array($wx_username, $wx_dyname));
        if(empty($this->db->wechat_trumpetrow($wx_dyname,'wx_name','id')))exitjson(-1,'店员数据不存在');
        $qrrow = $this->db->qrlistrow($wx_username,'beizhu','beizhu');
        if(empty($qrrow))exitjson(-1,'用户数据不存在');
        
        $data = [
            'status'=>1,
            'endtime'=>$this->date,
            'type'=>"wxpay",
            'wx_name'=>$wx_dyname
        ];
        
        if($this->db->update("qrlist",$data,['beizhu'=>$wx_username]))exitjson(200,'强制绑定成功！');
        exitjson(-1,'绑定失败！');
    }
    public function Wechat_Trumpetremove(){
        if($this->islogin_admin!=1)exitjson(-1,'请先登录');
        $id = input("post")['id'];
        
        if(empty($id))exitjson(-1,'非法操作！');
        if(!$this->db->wechat_trumpetrow($id,'id','id'))exitjson(-1,'店员数据不存在');
        if($this->db->delete("wechat_trumpet",['id'=>$id]))exitjson(200,'删除成功！');
        exitjson(-1,'删除失败！');
    }
    
    /**
     * 验证并准备数据
     * @param array $data 输入数据
     * @param array $requiredFields 必填字段
     * @return array 处理后的数据
     */
    private function validateAndPrepareData($data, $requiredFields)
    {
        // 验证必填字段
        foreach ($requiredFields as $field => $fieldName) {
            if (empty($data[$field])) {
                exitjson(-1, "{$fieldName}不能为空");
            }
        }
        if($this->db->wechat_trumpetrow($data['wx_user'] ,'wx_user','id'))exitjson(-1,'该微信账号已存在请勿重复添加！');
        // 准备基础数据
        $baseData = [
            'sid' => md5(time()),
            'login_time' => time(),
            'addtime' => $this->date,
            'status' => 0
        ];
        
        // 合并用户输入数据和基础数据
        $params = array_merge($data, $baseData);
        
        // 过滤空值
        return array_filter($params, function($value) {
            return $value !== '';
        });
    }
}

?>